﻿using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Data;
using Modelo;

namespace Business
{
    public class ConsultaBO
    {
        BDCampus2Entities db = new BDCampus2Entities();
        /// <summary>
        /// Insere ou Edita uma Consulta
        /// </summary>
        /// <param name="objConstVO"></param>
        /// <returns></returns>
        public int InserirOuEditar(ConsultaVO objConstVO)
        {
            Consulta n = (from o in db.Consulta
                          where o.ATRIBUTO1.Equals(objConstVO.Atributo1)
                          select o).FirstOrDefault();


            if (n != null)
            {
                n.ATRIBUTO2 = objConstVO.Atributo2;
                n.ATRIBUTO3 = objConstVO.Atributo3;
                n.ATRIBUTO4 = objConstVO.Atributo4;
                n.ATRIBUTO5 = objConstVO.Atributo5;
                n.ATRIBUTO6 = objConstVO.Atributo6;
                n.ATRIBUTO7 = objConstVO.Atributo7;
                n.ATRIBUTO8 = objConstVO.Atributo8;
                n.ATRIBUTO9 = objConstVO.Atributo9;
                db.Consulta.ApplyCurrentValues(n);
                db.SaveChanges();
            }
            else
            {
                n = new Consulta();
                n.ATRIBUTO2 = objConstVO.Atributo2;
                n.ATRIBUTO3 = objConstVO.Atributo3;
                n.ATRIBUTO4 = objConstVO.Atributo4;
                n.ATRIBUTO5 = objConstVO.Atributo5;
                n.ATRIBUTO6 = objConstVO.Atributo6;
                n.ATRIBUTO7 = objConstVO.Atributo7;
                n.ATRIBUTO8 = objConstVO.Atributo8;
                n.ATRIBUTO9 = objConstVO.Atributo9;
                db.AddToConsulta(n);
                db.SaveChanges();
            }

            return n.ATRIBUTO1;
        }

        /// <summary>
        /// Seleciona uma Consulta
        /// </summary>
        /// <param name="idConsulta"></param>
        /// <returns></returns>
        public ConsultaVO SelectConsulta(int idConsulta)
        {
            Consulta n = (from o in db.Consulta
                          where o.ATRIBUTO1.Equals(idConsulta)
                          select o).FirstOrDefault();

            ConsultaVO objConsVO = new ConsultaVO();

            if (n != null)
            {

                objConsVO.Atributo1 = n.ATRIBUTO1;
                objConsVO.Atributo2 = n.ATRIBUTO2;
                objConsVO.Atributo3 = n.ATRIBUTO3;
                objConsVO.Atributo4 = n.ATRIBUTO4;
                objConsVO.Atributo5 = n.ATRIBUTO5;
                objConsVO.Atributo6 = n.ATRIBUTO6;
                objConsVO.Atributo7 = n.ATRIBUTO7;
                objConsVO.Atributo8 = n.ATRIBUTO8;
                objConsVO.Atributo9 = n.ATRIBUTO9;
            }
            return objConsVO;
        }

        /// <summary>
        /// Inativar Consulta
        /// </summary>
        /// <param name="idConsulta"></param>
        public void InativarConsulta(int idConsulta)
        {
            Consulta n = (from o in db.Consulta
                          where o.ATRIBUTO1.Equals(idConsulta)
                          select o).FirstOrDefault();


            if (n != null)
            {

                n.ATRIBUTO10 = "false";
                db.SaveChanges();
            }
        }
        /// <summary>
        /// Seleciona uma Lista de Consulta
        /// </summary>
        /// <param name="Consulta"></param>
        /// <returns></returns>
        public List<ConsultaVO> SelectConsulta(string Consulta)
        {
            List<ConsultaVO> ListConsVO = new List<ConsultaVO>();

            List<Consulta> listC = (from o in db.Consulta
                                    where o.ATRIBUTO2.Contains(Consulta)
                                    select o).OrderByDescending(c => c.ATRIBUTO1).ToList();

            foreach (var item in listC)
            {
                ConsultaVO objConsVO = new ConsultaVO();
                objConsVO.Atributo1 = item.ATRIBUTO1;
                objConsVO.Atributo2 = item.ATRIBUTO2;
                objConsVO.Atributo3 = item.ATRIBUTO3;
                objConsVO.Atributo4 = item.ATRIBUTO4;
                objConsVO.Atributo5 = item.ATRIBUTO5;
                objConsVO.Atributo6 = item.ATRIBUTO6;
                objConsVO.Atributo7 = item.ATRIBUTO7;
                objConsVO.Atributo8 = item.ATRIBUTO8;
                objConsVO.Atributo9 = item.ATRIBUTO9;
                ListConsVO.Add(objConsVO);
            }

            return ListConsVO;
        }
    }
}
